package com.aplana.ccsd.web.template;

import info.magnolia.module.blossom.annotation.Area;
import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.dialog.TabBuilder;

import java.util.List;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aplana.ccsd.model.HorizontalBlock;
import com.aplana.ccsd.model.MenuItem;
import com.aplana.ccsd.service.HorizontalAreaService;
import com.aplana.ccsd.service.NavigationMenuService;
import com.aplana.ccsd.service.PromoService;

/**
 * Template for home page
 */
@Controller
@Template(title = "Aplana Home", id = "blossomCCSD:pages/home")
public class HomeTemplate {
	
	@SuppressWarnings("unused")
	private static final Logger LOG = LoggerFactory
			.getLogger(HomeTemplate.class);
	
	@Autowired
    private NavigationMenuService navigationMenuService;
	
	@Area("main")
    @Controller
    public static class MainArea {
        @TabFactory("Content")
        public void contentTab(TabBuilder tab) {
        }

        @RequestMapping("/homeTemplate/main")
        public String render() {
            return "areas/verticalArea.jsp";
        }
    }
	
	@Area("mainHorizontal")
	@Controller
	public static class MainHorizontalArea {
		
		@Autowired
		HorizontalAreaService horizontalAreaService;

	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    	horizontalAreaService.addHorizontalAreaDialogControls(tab);
	    }

	    @RequestMapping("/homeTemplate/mainHorizontal")
	    public String render(Node page, ModelMap model, HttpServletRequest request) throws PathNotFoundException, RepositoryException {	
			
	    	List<HorizontalBlock> horizontalComponents =  horizontalAreaService.getHorizontalBlocks(page);
	    	model.put("horizontalBlocks", horizontalComponents);
	    	
	        return "areas/horizontalArea.jsp";
	    }
	}
	
	@Area("mainHorizontalCenter")
	@Controller
	public static class MainHorizontalCenterArea {
		
		@Autowired
		HorizontalAreaService horizontalAreaService;

	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    	horizontalAreaService.addHorizontalAreaDialogControls(tab);
	    }

	    @RequestMapping("/homeTemplate/mainHorizontalCenter")
	    public String render(Node page, ModelMap model, HttpServletRequest request) throws PathNotFoundException, RepositoryException {	
			
	    	List<HorizontalBlock> horizontalComponents =  horizontalAreaService.getHorizontalBlocks(page);
	    	model.put("horizontalBlocks", horizontalComponents);
	    	
	    	com.aplana.ccsd.model.Area horizontalArea = horizontalAreaService.getHorizontalArea(page);;
	    	model.put("horizontalArea", horizontalArea);
	    	
	        return "areas/horizontalArea.jsp";
	    }
	}
	
	@Area("mainPromos")
	@Controller
	public static class PromosArea {
		@Autowired
		PromoService promoService;
		
	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    	promoService.addPromosAreaDialogControls(tab);
	    }

	    @RequestMapping("/homeTemplate/promos")
	    public String render(Node page, ModelMap model, HttpServletRequest request) throws PathNotFoundException, RepositoryException {
	    	com.aplana.ccsd.model.Area promosArea = promoService.getPromosArea(page);
	    	model.put("promosArea", promosArea);
	        return "areas/promos.jsp";
	    }
	}

    @RequestMapping("/homeTemplate")
    public String render(Node page, ModelMap model) throws RepositoryException {  
    	List<MenuItem> topMenus = navigationMenuService.getTopMenus(page);
        model.put("topMenus", topMenus);
        
        return "pages/home.jsp";
    }

    @TabFactory("Content")
    public void propertiesDialog(TabBuilder tab) {
        tab.addEdit("title", "Title", "");
        tab.addCheckbox("hideInNavigation", "Hide in navigation", "Check this box to hide this page in navigation");
    }
}
